﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Text;
using System.Threading.Tasks;

namespace WCSService.ToWCS
{
    public class SendRequest
    {
        private string _url = string.Empty;
        public SendRequest(string url)
        {
            _url = url;
        }
        public Response SendAgvSetGoodApply(string currentPosition, string method, string taskCode)
        {
            try
            {
                AgvSetGoodApply_Request request = new AgvSetGoodApply_Request
                {
                    currentPosition = currentPosition,
                    method = method,
                    reqCode = Guid.NewGuid().ToString(),
                    reqTime = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"),
                    taskCode = taskCode
                };
                string strRequest = Newtonsoft.Json.JsonConvert.SerializeObject(request);
                return SendHttpRequest(strRequest, "agvSetGoodApply");
            }
            catch (Exception ex)
            {
                return new Response { code = "1", message = "发生异常:" + ex.Message + ex.StackTrace };
            }
        }
        public Response SendAgvTaskStatus(string currentPosition, string method, string taskCode)
        {
            try
            {
                AgvTaskStatusCallback_Request request = new AgvTaskStatusCallback_Request
                {
                    currentPosition = currentPosition,
                    method = method,
                    reqCode = Guid.NewGuid().ToString(),
                    reqTime = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"),
                    taskCode = taskCode
                };
                string strRequest = Newtonsoft.Json.JsonConvert.SerializeObject(request);
                return SendHttpRequest(strRequest, "agvTaskStatusCallback");
            }
            catch (Exception ex)
            {
                return new Response { code = "1", message = "发生异常:" + ex.Message + ex.StackTrace };
            }
        }

        public Response SendAgvWarn(string beginDate, string warnContent, string taskCode)
        {
            try
            {
                AgvWarnCallback_Request request = new AgvWarnCallback_Request
                {
                    reqCode = Guid.NewGuid().ToString(),
                    reqTime = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss")
                };
                AgvWarn warn = new AgvWarn { beginDate = beginDate, warnContent = warnContent, taskCode = taskCode };
                request.data = warn;
                string strRequest = Newtonsoft.Json.JsonConvert.SerializeObject(request);
                return SendHttpRequest(strRequest, "agvWarnCallback");
            }
            catch (Exception ex)
            {
                return new Response { code = "1", message = "发生异常:" + ex.Message + ex.StackTrace };
            }
        }

        private Response SendHttpRequest(string strRequest, string context)
        {
            try
            {
                Service.WriteLog("发送：请求url[" + _url + context + "]" + strRequest);
                string postData = strRequest;
                byte[] dataArray = Encoding.UTF8.GetBytes(postData);
                HttpWebRequest request = WebRequest.Create(_url + context) as HttpWebRequest;
                if (string.IsNullOrEmpty(strRequest))
                    request.Method = "get";
                else
                    request.Method = "post";
                request.ContentType = "application/json";
                request.ContentLength = dataArray.Length;
                if (request.Method.ToString().ToLower() == "post")
                {
                    Stream dataStream = request.GetRequestStream();
                    //发送请求
                    dataStream.Write(dataArray, 0, dataArray.Length);
                    dataStream.Close();
                }
                using (HttpWebResponse response = request.GetResponse() as HttpWebResponse)
                {
                    StreamReader reader = new StreamReader(response.GetResponseStream());
                    string result = reader.ReadToEnd();
                    Service.WriteLog("接收：" + result);
                    return Newtonsoft.Json.JsonConvert.DeserializeObject<Response>(result);
                }
            }
            catch (Exception ex)
            {
                Service.WriteLog("发生异常：请求url[" + _url + context + "]" + ex.Message + ex.StackTrace);
                return new Response { code = "1", message = "连接服务器失败:" + ex.Message + ex.StackTrace };
            }
        }
    }
}
